package icbc.nav.control
{
	import com.greensock.TweenLite;
	import com.greensock.easing.Strong;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.URLRequest;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
	import icbc.app.AppGlobal;
	
	import org.osflash.signals.Signal;
	
	public class UpDownButton extends Sprite
	{
		public static var STYLE_ROOT:uint = 0;
		public static var STYLE_CHILD:uint = 1;
		private static const TXT_CHILD_NAME:String = "txt";
		private static const FONT_SIZE:uint = 10;
		private static const FONT_COLOR:uint = 0xE381B2;
		private static const ICON_WIDTH:uint = 39;
		private static const TEXT_HEIGHT:uint = 27;
		private var iconWidth:uint = ICON_WIDTH;
		private var gap:uint = 5;
		public var data:Object;
		private var controlWidth:uint;
		private var controlHeight:uint;
		private var icon:Sprite;
		private var rounded:uint = 20;
		public var state:uint = 0;
		private var baseRedColor:uint = 0xC52027;
		private var borderColor:uint = 0xAAAAAA;
		
		private var main:Sprite = new Sprite();
		private var base:Sprite = new Sprite();
		private var over:Sprite = new Sprite();
		private var press:Sprite = new Sprite();
		private var clicked:Sprite = new Sprite();
		private var panel:Sprite;
		private var style:uint = STYLE_CHILD;
		private var downSense:Boolean = false;
		
		public var onClicked:Signal = new Signal(UpDownButton);
		
		public function UpDownButton(data:Object, vWidth:uint, vHeight:uint, downSense:Boolean = false)
		{
			super();
			this.data = data;
			this.style = style;
			this.controlWidth = vWidth;
			this.controlHeight = vHeight;
			this.downSense = downSense;
			iconWidth = data.icon != null ? ICON_WIDTH : 0;
			addChild(main);
			main.addChild(base);
			main.addChild(over);
			main.addChild(clicked);
			main.addChild(press);
			addEventListener(Event.ADDED_TO_STAGE, initStage);
		}
		
		private function initStage(evt:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE, initStage);
			display();
		}
		
		private function display():void
		{
			buildBase();
			buildOver();
			buildClicked();
			buildPress();
			addListeners();
		}
		
		private function build(item:Sprite, bgColor:uint, textColor:uint = 0x1f416c):void
		{
			setListItemBase(item, bgColor, textColor);
		}
		
		private function setListItemBase(item:Sprite, bgColor:uint, textColor:uint = 0x1f416c):void
		{
			item.graphics.lineStyle(0.5, borderColor);
			item.graphics.beginFill(bgColor);
			item.graphics.drawRect(0, 0, controlWidth, controlHeight);
			item.graphics.endFill();
			
			var arrow:Sprite = buildArrowSymbol(textColor, downSense ? 0 : 1);
			item.addChild(arrow);
			arrow.x = controlWidth - arrow.width - 3;
			arrow.y = controlHeight/2 - arrow.height/2;
			
			var itemtxt:TextField = buildText( {
				width : controlWidth,
				height : 18,
				text : data.label,
				color : textColor
			} );
			//var txtFilterD:DropShadowFilter = new DropShadowFilter(1, 45, 0x999999, 1, 1, 1, 1, 3, false, false, false);
			//itemtxt.filters = [txtFilterD];
			itemtxt.name = TXT_CHILD_NAME;
			item.addChild(itemtxt);
			
			var posx:int = controlWidth/2 - itemtxt.width/2;
			if(style == STYLE_CHILD)
			{
				posx = 10;
			}
			itemtxt.x = posx;
			itemtxt.y = controlHeight/2 - itemtxt.height/2;
			item.alpha = 0;
		}
		
		private function buildText(config:Object) : TextField
		{
			var textSize:uint = FONT_SIZE;
			var textAlign:String = "center";
			var autoSize:String = TextFieldAutoSize.CENTER;
			if(style == STYLE_CHILD)
			{
				textSize = FONT_SIZE;
				textAlign = "left";
				autoSize = TextFieldAutoSize.LEFT;
			}
			var text:String = (config.text != undefined)?config.text:"";
			var width:uint = (config.width != undefined)?config.width:120;
			var height:uint = (config.height != undefined)?config.height:18;
			var color:uint = (config.color != undefined)?config.color:0xffffff;
			var format:TextFormat = new TextFormat();
			format.font = AppGlobal.FONT_NAME;
			format.color = color;
			format.size = textSize;
			format.align = textAlign;
			var txt:TextField = new TextField();
			txt.selectable = false;
			txt.defaultTextFormat = format;
			txt.autoSize = autoSize;
			txt.text = text;
			return txt;
		}
		
		private function addListeners():void
		{
			press.addEventListener(MouseEvent.MOUSE_OVER, onRollOver);
			press.addEventListener(MouseEvent.MOUSE_OUT, onRollOut);
			press.addEventListener(MouseEvent.MOUSE_DOWN, onPress);
			press.addEventListener(MouseEvent.MOUSE_UP, onRelease);
		}
		
		private function removeListeners():void
		{
			press.removeEventListener(MouseEvent.MOUSE_OVER, onRollOver);
			press.removeEventListener(MouseEvent.MOUSE_OUT, onRollOut);
			press.removeEventListener(MouseEvent.MOUSE_DOWN, onPress);
			press.removeEventListener(MouseEvent.MOUSE_UP, onRelease);
		}
		
		private function onRollOver(evt:Event):void
		{
			var tween:TweenLite = new TweenLite(over,1,{ease:Strong.easeOut,alpha:1});
		}
		
		private function onRollOut(evt:Event):void
		{
			var tween:TweenLite = new TweenLite(over,2,{ease:Strong.easeOut,alpha:0});
			press.alpha = 0;
		}
		
		private function onPress(evt:Event):void
		{
			var tween:TweenLite = new TweenLite(press,0.5,{ease:Strong.easeOut,alpha:1});
		}
		
		private function onRelease(evt:Event):void
		{
			var tween:TweenLite = new TweenLite(press,1,{ease:Strong.easeOut,alpha:0});
			onClicked.dispatch(this);
			clicked.alpha = 1;
		}
		
		private function buildBase():void
		{
			build(base, baseRedColor, 0xffffff);
			base.alpha = 1;
		}
		
		private function buildOver() : void
		{
			var textColor:uint = 0xffffff;
			build(over, 0xffffff, baseRedColor);
		}
		
		private function buildClicked() : void
		{
			var textColor:uint = 0xffffff;
			if(style == STYLE_CHILD)
			{
				textColor = baseRedColor;
			}
			build(clicked, 0xA8A8AA, textColor);
			clicked.visible = false;
		}
		
		private function buildPress() : void
		{
			build(press, 0xffffff, baseRedColor);
		}
		
		public function dispose():void
		{
			removeListeners();
		}
		
		private function buildArrowSymbol(color:uint, sense:uint):Sprite
		{
			var aHeight:uint = 5;
			var aWidth:uint = 16;
			var sprite:Sprite = new Sprite();
			sprite.graphics.lineStyle(1, color);
			if(sense == 0)
			{
				sprite.graphics.lineTo(0,0);
				sprite.graphics.lineTo(aWidth/2,aHeight);
				sprite.graphics.lineTo(aWidth,0);
			} else {
				sprite.graphics.moveTo(0,aHeight);
				sprite.graphics.lineTo(aWidth/2,0);
				sprite.graphics.lineTo(aWidth,aHeight);
			}
			return sprite;
		}
	}
}